package com.nondo.dean.community.controller.api.app;

import com.nondo.dean.auth.common.TokenPrincipalProvider;
import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.community.domain.Article;
import com.nondo.dean.community.domain.UserArticle;
import com.nondo.dean.community.model.ArticleBean;
import com.nondo.dean.community.service.UserArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author xiaofan
 * @Description:
 * @throws
 * @date 2018-05-0414:01
 * @Version 0.1
 */
@RestController("apiAppCommunityUserArticle")
@RequestMapping("/{v}/app/user/article")
public class UserArticleController {

    @Autowired
    UserArticleService userArticleService;

    @Autowired
    TokenPrincipalProvider tokenPrincipalProvider;

    /**
     * 添加收藏
     * @return view
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<Article> add(HttpServletRequest request, HttpServletResponse response,
                             @Validated @RequestBody UserArticle userArticle, BindingResult resul){
        View view = new View(20000, "收藏成功");
        if(resul.hasErrors()){
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(resul.getAllErrors()));
        }else{
            this.userArticleService.add(userArticle);
        }

        return view;
    }

    /**
     * 取消收藏
     * @return view
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<Article> update(HttpServletRequest request, HttpServletResponse response,
                             @Validated @RequestBody UserArticle userArticle, BindingResult resul){
        View view = new View(20000, "取消成功");
        if(resul.hasErrors()){
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(resul.getAllErrors()));
        }else{
            this.userArticleService.update(userArticle);
        }

        return view;
    }

    /**
     * 查询用户收藏文章
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/collect/list")
    public View<Page<ArticleBean>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<UserArticle> query) {
        View<Page<ArticleBean>> view = new View<Page<ArticleBean>>(20000, "获取数据成功");

        UserArticle userArticle = new UserArticle();
        userArticle.setStatus(1);
        userArticle.setUserId(1);//TODO tokenPrincipalProvider.getTokenPrincipal().getUserId().intValue()
        Page<ArticleBean> page = new Page<ArticleBean>(query.getPage(), query.getLimit(), userArticleService.count(userArticle));

        List<ArticleBean> gradeList = userArticleService.queryUserCollectArticles(query.getOffset(), query.getLimit());

        page.setContent(gradeList);

        view.setData(page);

        return view;
    }


}
